//
//  SLSetGoodsViewController.m
//  StarLight
//
//  Created by 吴涛 on 16/6/4.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLSetGoodsViewController.h"
#import "SLLabelFeild.h"
#import "SLNetWork.h"
#import "SLNearlyProductDetailViewModel.h"


@interface SLSetGoodsViewController ()

@property (nonatomic, strong) NSString *gid;

@property (nonatomic, strong) SLLabelFeild *goodsNameLF;

@property (nonatomic, strong) SLLabelFeild *startLF;

@property (nonatomic, strong) SLLabelFeild *finalLF;

@property (nonatomic, strong) SLLabelFeild *weightLF;

@property (nonatomic, strong) SLLabelFeild *pakgeLF;

@property (nonatomic, strong) SLLabelFeild *dateLF;

@property (nonatomic, strong) SLLabelFeild *noteLF;

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UIScrollView *mainScrollView;

@end

@implementation SLSetGoodsViewController

- (void)viewDidLoad{
  [super viewDidLoad];
  
  self.mainScrollView = [UIScrollView new];
  [self.view addSubview:self.mainScrollView];
  [self setUpUI];
  [self addConstrains];
  
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  self.title = @"货源详情";
  [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)endEdited{
  [self.view endEditing:YES];
}

- (void)showWithGid:(NSString *)gid{
  self.gid = gid;
  
  NSString *url = [NSString stringWithFormat:@"%@/goods/goodsinfo?gid=%@",baseUrl,gid];
  @weakify(self)
  [SLNetWork getDataWithRequest:url complete:^(id respond, NSError *error) {
    @strongify(self)
    SLGoodsDetailModel *model = [SLGoodsDetailModel mj_objectWithKeyValues:respond[@"data"]];
    self.goodsNameLF.contentField.text = model.hwmc;
    self.startLF.contentField.text = model.qsgk;
    self.finalLF.contentField.text = model.ddgk;
    self.weightLF.contentField.text = model.hwzw;
    self.pakgeLF.contentField.text = model.bzxs;
    self.dateLF.contentField.text = model.jzrq;
    self.noteLF.contentField.text = model.xxbz;
  }];
}


- (void)delectGoods{
  
  NSString *postUrl = [NSString backUrlWithService:@"goods" methodName:@"delmygoods"];
  NSDictionary *para = @{@"gid" : self.gid, @"hzid" : [SLNetWork backUid]};
  @weakify(self)
  [SLNetWork postDataWithParameters:para postUrl:postUrl complete:^(id respond, NSError *error) {
    @strongify(self)
    if (!error) {
      [[NSNotificationCenter defaultCenter] postNotificationName:kSetGoodsDismiss object:nil];
      [self.navigationController popViewControllerAnimated:YES];
    }
  }];
}


- (void)setUpUI{
  self.view.backgroundColor = [UIColor defeutBackgroudColor];
  
  [self.mainScrollView addSubview:self.contentView];
  [self.contentView addSubview:self.goodsNameLF];
  [self.contentView addSubview:self.startLF];
  [self.contentView addSubview:self.finalLF];
  [self.contentView addSubview:self.weightLF];
  [self.contentView addSubview:self.pakgeLF];
  [self.contentView addSubview:self.dateLF];
  [self.contentView addSubview:self.noteLF];
  
  UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(delectGoods)];
  self.navigationItem.rightBarButtonItem = rightItem;
  
}

- (void)addConstrains{
  
  CGFloat linePading = 15;
  
  CGFloat lfPading = 20;
  
  [self.mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.view);
  }];
  
  [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.mainScrollView);
    make.width.equalTo(self.mainScrollView);
  }];
  
  [self.goodsNameLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.width.equalTo(self.contentView);
    make.centerX.top.equalTo(self.contentView);
    make.height.mas_equalTo(50*MIX_H);
  }];
  
  
//  UIImageView *firLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
  UIView *firLine = [UIView xlineView];
  [self.view addSubview:firLine];
  [firLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.goodsNameLF.mas_bottom);
    make.left.equalTo(self.goodsNameLF);
    make.right.equalTo(self.goodsNameLF);
    make.height.equalTo(@1);
  }];
  
  
  [self.startLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(firLine.mas_bottom).offset(linePading);
    make.left.width.height.equalTo(self.goodsNameLF);
  }];
  
  [self.finalLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.startLF.mas_bottom).offset(lfPading);
    make.left.width.height.equalTo(self.goodsNameLF);
    
  }];
  
//  UIImageView *secondLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
  UIView *secondLine = [UIView xlineView];
  [self.contentView addSubview:secondLine];
  [secondLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.finalLF.mas_bottom).offset(linePading);
    make.left.equalTo(self.goodsNameLF);
    make.right.equalTo(self.goodsNameLF);
    make.height.equalTo(@1);
  }];
  
  
  [self.weightLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(secondLine.mas_bottom).offset(linePading);
    make.left.width.height.equalTo(self.finalLF);
  }];
  
  UIView *thirdLine = [UIView xlineView];
  [self.contentView addSubview:thirdLine];
  [thirdLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.weightLF.mas_bottom).offset(linePading);
    make.left.equalTo(self.goodsNameLF);
    make.right.equalTo(self.goodsNameLF);
    make.height.equalTo(@1);
  }];
  
  [self.pakgeLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.weightLF.mas_bottom).offset(lfPading);
    make.left.width.height.equalTo(self.finalLF);
  }];
  
  [self.dateLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.pakgeLF.mas_bottom);
    make.left.width.height.equalTo(self.finalLF);
  }];
  
  
//  UIImageView *forthLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
  UIView *forthLine = [UIView xlineView];
  [self.contentView addSubview:forthLine];
  [forthLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.dateLF.mas_bottom);
    make.left.equalTo(self.goodsNameLF);
    make.right.equalTo(self.goodsNameLF);
    make.height.equalTo(@1);
  }];
  
  [self.noteLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(forthLine.mas_bottom).offset(linePading);
    make.left.height.equalTo(self.finalLF);
    make.right.equalTo(self.goodsNameLF);
  }];
  
  [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
    make.bottom.equalTo(self.noteLF);
  }];
}

#pragma  mark - getter

- (UIView *)contentView{
  if (!_contentView) {
    _contentView = [UIView new];
    _contentView.backgroundColor = [UIColor whiteColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEdited)];
    [_contentView addGestureRecognizer:tap];
  }
  return _contentView;
}

- (SLLabelFeild *)noteLF{
  if (!_noteLF) {
    _noteLF = [[SLLabelFeild alloc] initWithTitle:@"其他备注:" textViewHolder:@"     "];
    _noteLF.limit = 200;
    [_noteLF enableNo];
  }
  return _noteLF;
}

- (SLLabelFeild *)dateLF{
  if (!_dateLF) {
    _dateLF = [[SLLabelFeild alloc] initWithTitle:@"日期:" fieldPlaceHolder:@"    "];
    [_dateLF enableNo];
  }
  return _dateLF;
}

- (SLLabelFeild *)pakgeLF{
  if (!_pakgeLF) {
    _pakgeLF = [[SLLabelFeild alloc] initWithTitle:@"包装形式:" fieldPlaceHolder:@"     "];
    [_pakgeLF enableNo];
  }
  return _pakgeLF;
}

- (SLLabelFeild *)weightLF{
  if (!_weightLF) {
    _weightLF = [[SLLabelFeild alloc] initWithTitle:@"重量:" fieldPlaceHolder:@"     "];
    _weightLF.contentField.keyboardType = UIKeyboardTypeNumberPad;
    [_weightLF enableNo];
  }
  return _weightLF;
}

- (SLLabelFeild *)finalLF{
  if (!_finalLF) {
    _finalLF = [[SLLabelFeild alloc] initWithTitle:@"目的码头:" fieldPlaceHolder:@"    "];
    _finalLF.limit = 15;
    [_finalLF enableNo];
  }
  return _finalLF;
}

- (SLLabelFeild *)startLF{
  if (!_startLF) {
    _startLF = [[SLLabelFeild alloc] initWithTitle:@"装货码头:" textViewHolder:@"    "];
    _startLF.limit = 15;
    [_startLF enableNo];
  }
  return _startLF;
}


- (SLLabelFeild *)goodsNameLF{
  if (!_goodsNameLF) {
    _goodsNameLF = [[SLLabelFeild alloc] initWithTitle:@"货源名称:" fieldPlaceHolder:@"    "];
    _goodsNameLF.limit = 8;
    [_goodsNameLF enableNo];
  }
  return _goodsNameLF;
}




- (UIScrollView *)mainScrollView{
  if (!_mainScrollView) {
    _mainScrollView = [UIScrollView new];
  }
  return _mainScrollView;
}


@end
